package com.lmk.mkcc.mq.consumer;

import com.alibaba.fastjson.JSON;
import com.lmk.mkcc.domain.MediaFile;
import com.lmk.mkcc.service.IMediaFileService;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

@Component
@RocketMQMessageListener(
        topic = "topic-coursevideo",
        selectorExpression = "tag-coursevideopush",
        consumerGroup = "push-coursevideo-group",
        // 消息模式
        messageModel = MessageModel.CLUSTERING)
public class PushVideoConsumer implements RocketMQListener<MessageExt> {
    @Autowired
    private IMediaFileService mediaFileService;

    @Override
    public void onMessage(MessageExt messageExt) {
        // 将body数组转化为字符串
        String s = new String(messageExt.getBody(), StandardCharsets.UTF_8);
        // 将字符串转化为对象
        MediaFile mediaFile = JSON.parseObject(s, MediaFile.class);
        // 推流
        mediaFileService.handleFile2m3u8(mediaFile);

    }
}
